package com.ly.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.reggie.dto.DishDto;
import com.ly.reggie.mapper.DishMapper;
import com.ly.reggie.pojo.Dish;
import com.ly.reggie.pojo.DishFlavor;
import com.ly.reggie.service.DishFlavorService;
import com.ly.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    @Override
    /**
     * 新增菜品，同时插入菜品对应的口味，需要操作两张表：Dish，DishFlavor
     * @param dishDto
     */
    @Transactional//开启事务注解
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到菜品表
        this.save(dishDto);

        //保存口味数据到口味表
        Long id = dishDto.getId();//获取菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();//获取菜品口味
        flavors = flavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());//为菜品添加id,并且专成一个新的集合

        //保存菜品口味
        dishFlavorService.saveBatch(flavors);
    }


    /**
     * 获取菜品，同时插入菜品对应的口味，需要操作两张表：Dish，DishFlavor
     *
     * @param id
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);

        //新建一个DishDto对象
        DishDto dishDto = new DishDto();

        //将dish拷贝到dishDto中
        BeanUtils.copyProperties(dish, dishDto);

        //构造LambdaQueryWrapper表达式，根据id查询菜品口味
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavorsList = dishFlavorService.list(lambdaQueryWrapper);

        dishDto.setFlavors(flavorsList);

        return dishDto;
    }

    @Override
    /**
     * 修改菜品，同时插入菜品对应的口味，需要操作两张表：Dish，DishFlavor
     * @param dishDto
     */
    @Transactional//开启事务注解
    public void updateWithFlavor(DishDto dishDto) {
        //更新菜品数据
        this.updateById(dishDto);
        //清理当前菜品对应的口味数据
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);

        //增加新的口味数据
        Long id = dishDto.getId();//获取菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();//获取菜品口味
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());//为菜品添加id,并且专成一个新的集合

        dishFlavorService.saveBatch(flavors);
    }
}
